package com.wan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wan.entity.GameAnnouncement;
import com.wan.entity.GameFriend;
import com.wan.entity.GameUser;
import com.wan.service.GameAnnouncementService;
import com.wan.service.GameFriendService;
import com.wan.service.GameUserService;
import com.wan.service.system.LivingBuildingService;
import com.wan.service.system.OfficeBuildingService;
import entity.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 生活楼信息表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-03-11
 */
@Controller
@RequestMapping("/map/livingBuilding")
public class LivingBuildingController {
    private static final long PAGE_SIZE = 10;
    @Autowired
    private LivingBuildingService livingBuildingService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private GameUserService gameUserService;
    @Autowired
    private GameAnnouncementService announcementService;
    @Autowired
    private GameFriendService friendService;

    @RequestMapping("/file/upload")
    @ResponseBody
    public ResultBean officeFileUpload(MultipartFile file) {
        ResultBean resultBean = officeBuildingService.fileUpload(file);
        return resultBean;
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultBean officeBuildSave(String accountNumber,String password,String nickName,String imageFile,Integer level,String signGe) {
        ResultBean resultBean = livingBuildingService.livingBuildSave(accountNumber,password,nickName,imageFile,level,signGe);
        return resultBean;
    }

    @RequestMapping("/search/list")
    @ResponseBody
    public ResultBean searchList(Long currPage,String title){
        IPage<GameUser> page = new Page<>(currPage,PAGE_SIZE);
        QueryWrapper<GameUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("nick_name",title);
        queryWrapper.orderByDesc("create_time");
        IPage<GameUser> livingBuildingIPage = gameUserService.page(page,queryWrapper);
        List<GameUser> pageRecords = livingBuildingIPage.getRecords();
        Long count = livingBuildingIPage.getTotal();
        long totalPage  = (count + PAGE_SIZE -1) / PAGE_SIZE;
        return new ResultBean(0,"成功",totalPage,pageRecords);
    }


    @RequestMapping("/edit")
    @ResponseBody
    public ResultBean updateOffice(Integer id,String accountNumber,String imageFile,String nickName,String password) {
        ResultBean resultBean = livingBuildingService.updateLivingBuild(id,accountNumber,imageFile,nickName,password);
        return resultBean;
    }

    @RequestMapping("/delete/gonggao")
    @ResponseBody
    public ResultBean deleteGongGao(Integer livingId){
        Boolean b = announcementService.removeById(livingId);
        if (b) {
            return new ResultBean(200,"删除成功");
        }else {
            return new ResultBean(500,"删除失败");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultBean delete(Integer livingId){
        Boolean b = gameUserService.removeById(livingId);
        QueryWrapper<GameFriend> friendQueryWrapper = new QueryWrapper<>();
        friendQueryWrapper.eq("user_id",livingId);

        friendService.remove(friendQueryWrapper);
        if (b) {
            return new ResultBean(200,"删除成功");
        }else {
            return new ResultBean(500,"删除失败");
        }
    }


    @RequestMapping("/delete/batch")
    @ResponseBody
    public ResultBean deleteBatch(String livingIds){
        String[] strings = livingIds.split(",");
        for (String id : strings){
            Boolean b = gameUserService.removeById(id);
        }
        return new ResultBean(200,"删除成功");
    }

}

